﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Terralever.Facebook.UI.Design;
using System.ComponentModel;
using System.Globalization;

namespace Terralever.Facebook.UI.FbmlControls
{
    /// <summary>
    /// Renders a pronoun for the specified user.
    /// </summary>
    [ToolboxData("<{0}:Pronoun runat=\"server\" />")]
    [Designer("Terralever.Facebook.UI.Design.FbmlControls.FbPronounDesigner, " + Constants.DESIGNER_LIB)]
    public class Pronoun : FbmlControl
    {
        /// <summary>
        /// Gets or sets the Facebook User ID of the user to display.  This property is required to be set.
        /// </summary>
        [FbmlRequired(IsRequired = true)]
        [Category(FbmlConstants.FBML_REQD_ATTR)]
        [Description("The Facebook User ID of the user for whom this name control is rendered.")]
        [DefaultValue(0)]
        public long FbUserID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets whether to render the user's name as if it was possessive.
        /// </summary>
        [Category(FbmlConstants.FBML_OPT_ATTR)]
        [Description("Uses the possessive form (his/her/your/their).")]
        [DefaultValue(false)]
        public bool Possessive { get; set; }

        /// <summary>
        /// Gets or sets whether to be reflexive if the <see>UseYou</see> property is set to <see langword="true" />.
        /// </summary>
        [Category(FbmlConstants.FBML_OPT_ATTR)]
        [Description("Uses the reflexive form (himself/herself/yourself/themselves).")]
        [DefaultValue(false)]
        public bool Reflexive { get; set; }

        /// <summary>
        /// Gets or sets whether to be reflexive if the <see>UseYou</see> property is set to <see langword="true" />.
        /// </summary>
        [Category(FbmlConstants.FBML_OPT_ATTR)]
        [Description("Uses the objective form (him/her/you/them).")]
        [DefaultValue(false)]
        public bool Objective { get; set; }

        /// <summary>
        /// Gets or sets whether to display "you" if the logged-in user matches the FbUserID property.
        /// </summary>
        [Category(FbmlConstants.FBML_OPT_ATTR)]
        [Description("Displays \"you\" if the logged-in user matches the FbUserID property.")]
        [DefaultValue(true)]
        public bool UseYou { get; set; }

        /// <summary>
        /// Gets or sets whether to display "you" if the logged-in user matches the FbUserID property.
        /// </summary>
        [Category(FbmlConstants.FBML_OPT_ATTR)]
        [Description("Displays \"they\" if user's gender is not specified.")]
        [DefaultValue(true)]
        public bool UseThey { get; set; }

        /// <summary>
        /// Gets or sets whether to capitalize "You" or its variants if the <see>UseYou</see> property is <see langword="true" />
        /// </summary>
        [Category(FbmlConstants.FBML_OPT_ATTR)]
        [Description("Capitalizes the pronoun.")]
        [DefaultValue(false)]
        public bool Capitalize { get; set; }

        /// <inheritdoc />
        protected internal override string ElementName
        {
            get { return FbmlConstants.FB_PRONOUN; }
        }

        /// <inheritdoc />
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (FbUserID == 0)
                throw new RequiredFbmlAttributeNotSetException("FbUserID", FbUserID);

            writer.AddAttribute("uid", FbUserID.ToString(CultureInfo.InvariantCulture));

            if (Possessive)
                writer.AddAttribute("possessive", FbmlConstants.TRUE);
            if (Reflexive)
                writer.AddAttribute("reflexive", FbmlConstants.TRUE);
            if (Objective)
                writer.AddAttribute("objective", FbmlConstants.TRUE);

            if (!UseYou)
                writer.AddAttribute("useyou", FbmlConstants.FALSE);
            if (!UseThey)
                writer.AddAttribute("usethey", FbmlConstants.FALSE);

            if (Capitalize)
                writer.AddAttribute("capitalize", FbmlConstants.TRUE);

            base.AddAttributesToRender(writer);
        }
    }
}
